package com.summer.springboot.mybatisplus.multiple.config;

import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

@Slf4j
@Configuration
@AutoConfigureAfter({DataSourceConfig.class})
@MapperScan(basePackages = {"com.summer.springboot.mybatisplus.multiple.mapper"})
public class MyBatisConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("myRoutingDataSource") MyRoutingDataSource myRoutingDataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "mybatisSqlSessionFactoryBean")
    @Primary
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("myRoutingDataSource") MyRoutingDataSource myRoutingDataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        Set<Resource> resources = new LinkedHashSet<>(16);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            if(resolver.getResources("classpath:mapper/*.xml") != null) {
                resources.addAll(Arrays.asList(resolver.getResources("classpath:mapper/*.xml")));
            }
//            if(resolver.getResources("classpath:config/mapper/*/*.xml") != null) {
//                resources.addAll(Arrays.asList(resolver.getResources("classpath:config/mapper/*/*.xml")));
//            }
//            if(resolver.getResources("classpath:mapoper/*/*.xml") != null) {
//                resources.addAll(Arrays.asList(resolver.getResources("classpath:mapoper/*/*.xml")));
//            }

        }catch (IOException e) {
            log.error("获取资源失败",e);
        }
        bean.setMapperLocations(resources.toArray(new Resource[0]));
        bean.setDataSource(myRoutingDataSource);
        bean.setVfs(SpringBootVFS.class);
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setLogImpl(StdOutImpl.class);
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        // 乐观锁
//        mybatisConfiguration.addInterceptor(optimisticLockerInterceptor());
        bean.setConfiguration(mybatisConfiguration);
        GlobalConfig globalConfig = GlobalConfigUtils.defaults();
        // 字段自动填充处理
//        globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
        bean.setGlobalConfig(globalConfig);
        return bean;
    }
}
